package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.ProcessLogFileDao;
import cn.topevery.garden.entity.po.ProcessLogFile;
import cn.topevery.garden.entity.query.ProcessLogFileQuery;
import cn.topevery.garden.entity.vo.ProcessLogFileVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.DbCommand;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlDeleteMate;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 过程日志 附件表
 *
 * @author code-generate
 * @date 2022-02-17
 */
@Repository
public class ProcessLogFileDaoImpl extends BaseDaoImpl<ProcessLogFile> implements ProcessLogFileDao {
    @Override
    public <P extends DbCsp> List<ProcessLogFile> getSearch(P para) {
        return null;
    }

    

    @Override
    public DataPager<ProcessLogFileVO> listPage(ProcessLogFileQuery query) {
        String table = "t_process_log_file tplf";

        String columns = "tplf.*";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tplf.c_id desc");
        sqlQueryMate
                .where(true, "tplf.c_db_status", ScriptConditionEnum.Eq, 0)
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, ProcessLogFileVO.class);
    }

    @Override
    public ProcessLogFile getEntityByColumn(String key,String value){
        KeyValue<String> map=new KeyValue<>();
        map.key=key;
        map.value=value;
        return getEntityByColumnEqValue(map);
    }

    @Override
    public List<ProcessLogFile> getListByMainId(Integer mainId) {
        SqlQueryMate sqlQueryMate = new SqlQueryMate("t_process_log_file tplf").setColumns("tplf.*");
        sqlQueryMate.where(true, "tplf.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, "tplf.c_main_id", ScriptConditionEnum.Eq, mainId);
        return selectList(sqlQueryMate, ProcessLogFile.class);
    }

    @Override
    public void deleteByMainId(Integer mainId) {
        String sql = "delete from t_process_log_file tplf where c_main_id = " + mainId;
        run(sql, CommandType.Text, DbCommand::executeNonQuery);
    }

    @Override
    public void deleteByMainIds(Integer[] mainIds) {
        SqlDeleteMate sqlDeleteMate = new SqlDeleteMate("t_process_log_file tplf");
        sqlDeleteMate.where(true, "c_main_id", ScriptConditionEnum.In, mainIds);
        String sql = sqlDeleteMate.getSql();
        run(sqlDeleteMate.getSql(), CommandType.Text, dbCommand -> {
            sqlDeleteMate.getParameters().forEach(dbCommand::addParameter);
            dbCommand.executeNonQuery();
        });
    }
}
